/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.controller;


import com.lvyh.lightframe.job.enums.ErrorCode;
import com.lvyh.lightframe.job.common.Result;
import com.lvyh.lightframe.job.admin.service.UserLoginService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lvyh 2021/04/11.
 */
@Controller
public class IndexController {
    private static Logger logger = LoggerFactory.getLogger(IndexController.class.getName());

    @Resource
    private UserLoginService userLoginService;

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {
        return "redirect:/task";
    }

    @RequestMapping("/toLogin")
    public String toLogin(Model model, HttpServletRequest request) throws Exception {
        if (userLoginService.checkUserLogin(request) != null) {
            return "redirect:/";
        }
        return "login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password, String ifRemember) throws Exception {
        if (userLoginService.checkUserLogin(request) != null) {
            return Result.create();
        }

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
            Result.create(ErrorCode.FAILED, "Account or password is empty.");
        }
        boolean ifRem = (StringUtils.isNotBlank(ifRemember) && "on".equals(ifRemember)) ? true : false;

        return userLoginService.login(response, userName, password, ifRem);
    }

    @RequestMapping(value = "logout", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (userLoginService.checkUserLogin(request) != null) {
            userLoginService.logout(request, response);
        }
        return Result.create();
    }

}
